<?php
//邮件服务器配置
DEFINE('NCMAIL_EMAIL' , 'no-reply@dadipedia.com');
define('NCMAIL_DOMAIN' , 'qq.com');
define('NCMAIL_USERNAME' , '125229322');
define('NCMAIL_PASS' , 'hqm_hqm');


defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
defined('YII_DEBUG') or define('YII_DEBUG',true);
$config = include_once('config.php');

$app=Yii::createConsoleApplication($config);
$app->commandRunner->addCommands(YII_PATH.'/cli/commands');
$env=@getenv('YII_CONSOLE_COMMANDS');
if(!empty($env))
	$app->commandRunner->addCommands($env);

$connecton = Yii::app()->db;
$sql = "select * from nc_emailqueue where flag = 'sending'";
$emailList = $connecton->createCommand($sql)->queryAll();
if(empty($emailList)) exit;
for($i = 0,$num = count($emailList);$i<$num;$i++){

	//发送邮件
	include_once('PHPMailer.php');
	$mail = new PHPMailer(); 
	$mail->IsSMTP(); // set mailer to use SMTP 
	$mail->Host = "smtp.".NCMAIL_DOMAIN.";"; // specify main and backup server 
	$mail->SMTPAuth = true; // turn on SMTP authentication 
	$mail->Username = NCMAIL_USERNAME; // SMTP username 
	$mail->Password = NCMAIL_PASS; // SMTP password 
	$mail->From = NCMAIL_EMAIL; 
	$mail->FromName = NCMAIL_USERNAME; 
	$mail->AddAddress($emailList[$i]['email']); // name is optional 
	$mail->AddReplyTo(NCMAIL_EMAIL, "Information");
	$mail->WordWrap = 50; // set word wrap to 50 characters 
	$mail->IsHTML(true); // set email format to HTML
	$mail->Subject = $emailList[$i]['subject']; 
	$mail->Body = $emailList[$i]['content'];
	$mail->AltBody = "这是一封来自NewsBag的邮件";
	$tag = $mail->Send();
	
	if($tag){
		echo 'send succuss!';
		$sql = "update nc_emailqueue set `flag` = 'sended', `send_time` = '".time() . "' where `id` = ".$emailList[$i]['id'];
		$connecton->createCommand($sql)->execute();
	} else{
		echo '<pre>';
		print_r($mail->ErrorInfo);
		$sql = "update nc_emailqueue set flag = 'error'";
		$connecton->createCommand($sql)->execute();
	}
}



?>